package com.gagakuai.bpm.domian.events;


import com.gagakuai.bpm.dao.mapper.camunda.ActHiActinstMapper;
import com.gagakuai.bpm.utils.RedisUtil;
import org.camunda.bpm.engine.HistoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/*
 * @program: bpm_server
 * @description: 更改提交时间
 * @author: houhong
 * @create: 2022-04-27 18:53
 */
@Component
public class UpDateStartListener implements ApplicationListener<UpdateStartNodeCreateEvent> {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private HistoryService historyService;

    @Resource
    private ActHiActinstMapper actHiActinstMapper;

    private Logger logger = LoggerFactory.getLogger(UpDateStartListener.class);

    @Override
    public void onApplicationEvent(UpdateStartNodeCreateEvent upteStartNodeCreateEvent) {

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("#更改发起时间，减少1s#");

        String processInstanceId = upteStartNodeCreateEvent.getProcessInstanceId();
        //修改第一个节点的时间
        actHiActinstMapper.updateStartNodetartTime(processInstanceId);

       /* String redisKey = BpmMetaConstants.BPM_COMPLETED_TYPE_GLOBAL
                + BpmMetaConstants.REDIS_FILE_KEY_SEPTOR + processInstanceId;

        Map<Object, Object> hmget = redisUtil.hmget(redisKey);


        String originTaskOwnersStr = (String) hmget.get("originTaskOwnersStr");

        String originTaskOwnersStrNotDelegate = (String) hmget.get("originTaskOwnersStrNotDelegate");

        String lastTaskOwnerStr = (String) hmget.get("lastTaskOwnerStr");

        String lastTaskDefKey = (String) hmget.get("lastTaskDefKey");

        String taskDefKey = (String) hmget.get("taskDefKey");


        //初始化 任务拥有者
        if (StringUtils.isEmpty(lastTaskOwnerStr) && StringUtils.isNonEmpty(lastTaskDefKey)) {


            List<HistoricTaskInstance> historyTaskList = historyService.createHistoricTaskInstanceQuery()
                    .processInstanceId(processInstanceId)
                    .taskDefinitionKey(lastTaskDefKey)
                    .list();

            StringBuffer lastTaskOwnerBuffer = new StringBuffer();
            historyTaskList.stream().map(HistoricTaskInstance::getAssignee).forEach(assigne -> {
                lastTaskOwnerBuffer.append(BpmMetaConstants.REDIS_CONDITION_KEY_SEPTOR)
                        .append(assigne);
            });
            hmget.put("lastTaskOwnerStr", lastTaskOwnerBuffer.toString());
        }

        if (StringUtils.isEmpty(originTaskOwnersStr) && StringUtils.isNonEmpty(taskDefKey)) {


            List<HistoricTaskInstance> historyTaskList = historyService.createHistoricTaskInstanceQuery()
                    .processInstanceId(processInstanceId)
                    .taskDefinitionKey(taskDefKey)
                    .list();

            StringBuffer thisTaskOwnerBuffer = new StringBuffer();
            historyTaskList.stream().map(HistoricTaskInstance::getAssignee).forEach(assigne -> {
                thisTaskOwnerBuffer.append(BpmMetaConstants.REDIS_CONDITION_KEY_SEPTOR)
                        .append(assigne);
            });
            hmget.put("originTaskOwnersStr", thisTaskOwnerBuffer.toString());
        }

        if (StringUtils.isEmpty(originTaskOwnersStrNotDelegate) && StringUtils.isNonEmpty(taskDefKey)) {


            List<HistoricTaskInstance> historyTaskList = historyService.createHistoricTaskInstanceQuery()
                    .processInstanceId(processInstanceId)
                    .taskDefinitionKey(taskDefKey)
                    .list();

            StringBuffer thisTaskOwnerBuffer = new StringBuffer();
            historyTaskList.stream().map(HistoricTaskInstance::getAssignee).forEach(assigne -> {
                thisTaskOwnerBuffer.append(BpmMetaConstants.REDIS_CONDITION_KEY_SEPTOR)
                        .append(assigne);
            });
            hmget.put("originTaskOwnersStrNotDelegate", thisTaskOwnerBuffer.toString());
        }

        redisUtil.hmset(redisKey, hmget);*/
    }
}